<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 系统广告服务
 */
class Advert_service extends MY_Service {

    public function __construct() {
        parent::__construct();
        log_message('debug', 'Advert_service Service Class Initialized');
    }

    /**
     * 更新需要在前台显示的广告内容缓存
     */
    public function update_advert_cache() {
        $this->load->driver('cache');
        $this->cache->file->save('advert', $this->get_advert_cache_value(), 86400);
    }

    /**
     * 获取需要在前台显示的广告内容
     *
     * @return array
     */
    public function get_advert_cache_value() {
        $cache = array();

        $this->load->model('advert_model');
        $cates = $this->advert_model->get_advert_cates(null, null, array('status' => 1));
        $adverts = $this->advert_model->get_adverts(null, null, array('status' => 1));
        foreach ($adverts as $advert) {
            //广告位是否不存在 或 广告是否过期
            if (!isset($cates[$advert['ckey']]) || ($advert['etime'] && $advert['etime'] < TIMESTAMP)) continue;

            //广告位显示方式
            if (!isset($cache[$advert['ckey']]['config'])) {
                $cache['config'][$advert['ckey']] = $cates[$advert['ckey']]['config']['display'];
            }

            $ad = array();
            $code = $jspop = $style = '';
            $conf = &$advert['config'];

            if ($conf['type'] == 'code') {
                $code = $conf['htmlcode'];
            } elseif ($conf['type'] == 'txt') {
                isset($conf['color']) && $conf['color'] && $style .= "color:{$conf['color']};";
                isset($conf['size']) && $conf['size'] && $style .= "font-size:{$conf['size']};";
                $style && $style = " style=\"{$style}\" ";
                $code = "<a href=\"{$conf['link']}\" target=\"_blank\"{$style}>{$conf['title']}</a>";
            } elseif ($conf['type'] == 'img') {
                isset($conf['width']) && $conf['width'] && $style .= " width=\"{$conf['width']}\"";
                isset($conf['height']) && $conf['height'] && $style .= " height=\"{$conf['height']}\"";
                isset($conf['title']) && $conf['title'] && $style .= " alt=\"{$conf['title']}\"";
                //TODO:多图切换显示
                $code = "<a title=\"{$conf['title']}\" href=\"{$conf['link']}\" target=\"_blank\"><img src=\"{$conf['url']}\"{$style}></a>";
            } elseif ($conf['type'] == 'flash') {
                isset($conf['width']) && $conf['width'] && $style .= " width=\"$conf[width]\"";
                isset($conf['height']) && $conf['height'] && $style .= " height=\"$conf[height]\"";
                $code = "<embed src=\"{$conf['link']}\" type=\"application/x-shockwave-flash\"{$style} wmode=\"opaque\" allowScriptAccess=\"always\"></embed>";
            }

            $ad['stime'] = $advert['stime'];
            $ad['etime'] = $advert['etime'];
            $ad['code'] = $jspop ? array('other' => $jspop, 'code' => $code) : $code;
            $cache[$advert['ckey']][$advert['id']] = $ad;
        }

        return $cache;
    }
}

/* End of file Advert_service.php */
/* Location: ./application/services/advert/Advert_service.php */
